using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class AndroidOperationUI : BaseUI
{
    private ButtonExpand m_AttackB;
    private ButtonExpand m_IntersectionB;
    private ButtonExpand m_TakeB;
    private ButtonExpand m_MapB;
    private FixedJoystick m_MoveJoyStick;
    
    private bool m_IsAttacking = false;
    private void Awake()
    {
        m_AttackB = UtilsUnity.GetChildComponent<ButtonExpand>(transform, "AttackB");
        m_IntersectionB = UtilsUnity.GetChildComponent<ButtonExpand>(transform, "IntersectionB");
        m_TakeB = UtilsUnity.GetChildComponent<ButtonExpand>(transform, "TakeB");
        m_MapB = UtilsUnity.GetChildComponent<ButtonExpand>(transform, "MapB");
        m_MoveJoyStick = UtilsUnity.GetChildComponent<FixedJoystick>(transform, "MoveJoystick");
        
        m_AttackB.onPointerDown.AddListener(OnAttackBDown);
        m_AttackB.onPointerUp.AddListener(OnAttackBUp);
        m_IntersectionB.onClick.AddListener(OnIntersectionBClicked);
        m_MapB.onClick.AddListener(OnMapBClicked);
        m_TakeB.onClick.AddListener(OnTakeBClicked);
    }
    void Update()
    {
        var playerQuiQui = WorldManager.Instance.GetPlayerQuiQui();
        if (playerQuiQui)
        {
            m_TakeB.gameObject.SetActive(playerQuiQui.lifeCycle >= QuiQuiObj.LifeCycleType.Boy);
            m_IntersectionB.gameObject.SetActive(playerQuiQui.lifeCycle >= QuiQuiObj.LifeCycleType.Youth);
            m_AttackB.gameObject.SetActive(playerQuiQui.lifeCycle >= QuiQuiObj.LifeCycleType.Prim);
            m_MapB.gameObject.SetActive(playerQuiQui.lifeCycle >= QuiQuiObj.LifeCycleType.Middle);
        }
        else
        {
            m_TakeB.gameObject.SetActive(false);
            m_IntersectionB.gameObject.SetActive(false);
            m_AttackB.gameObject.SetActive(false);
        }
        
        var playerController = WorldManager.Instance.GetPlayerControle();
        if (playerController)
        {
            playerController.DoAttack(m_IsAttacking);
            playerController.Move(m_MoveJoyStick.Direction.normalized);
        }
        
    }

    public void SetIntersectioinBInteractable(bool isInteractable)
    {
        m_IntersectionB.interactable = isInteractable;
    }
    public void SetTakeBInteractable(bool isInteractable)
    {
        m_TakeB.interactable = isInteractable;
    }

    void OnAttackBDown(PointerEventData eventData)
    {
        m_IsAttacking = true;
    }
    void OnAttackBUp(PointerEventData eventData)
    {
        m_IsAttacking = false;
    }
    void OnIntersectionBClicked()
    {
        var playerController = WorldManager.Instance.GetPlayerControle();
        if (!playerController)
            return;
        playerController.DoIntersection();
    }
    void OnMapBClicked()
    {
        var playerController = WorldManager.Instance.GetPlayerControle();
        if (!playerController)
            return;
        playerController.DoMap();
    }
    void OnTakeBClicked()
    {
        var playerController = WorldManager.Instance.GetPlayerControle();
        if (!playerController)
            return;
        playerController.DoTake();
    }
}
